package com.lanou.sfu.songforu.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.datas.AlbumMessageEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by quyan on 15/10/29.
 */
public class AlbumMessageFragment extends BaseFragment implements View.OnClickListener {
    private ImageView albumMessageBack,albumSearching,albumBigImg;
    private TextView albumMessageTitle,albumName,publishTime,albumType,albumLanguage,albumBrief;
    private String url;
    private AlbumMessageEntity.DataEntity data;
    private VolleySingleton volleySingleton;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mv_message,null);
        albumBigImg = (ImageView) view.findViewById(R.id.album_big_img);
        albumMessageBack = (ImageView) view.findViewById(R.id.album_message_back);
        albumSearching = (ImageView) view.findViewById(R.id.album_searching);
        albumMessageTitle = (TextView) view.findViewById(R.id.album_message_title);
        albumName = (TextView) view.findViewById(R.id.album_name);
        publishTime = (TextView) view.findViewById(R.id.pushing_time);
        albumType = (TextView) view.findViewById(R.id.album_type);
        albumLanguage = (TextView) view.findViewById(R.id.album_language);
        albumBrief = (TextView) view.findViewById(R.id.album_brief);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        albumMessageBack.setOnClickListener(this);
        albumSearching.setOnClickListener(this);
        Bundle bundle = getArguments();
        int id = bundle.getInt("id");
        url = "http://api.dongting.com/song/album/" + id + "?f=3030&os=4.2.2&alf=702006&imei=862136025639005&from=android&resolution=720x1280&net=2&api_version=1.0&agent=none&v=v8.2.0.2015091720&utdid=U157OqK3fvMBAPX2Buw9EsPo&address=%E8%BE%BD%E5%AE%81%E7%9C%81%E5%A4%A7%E8%BF%9E%E5%B8%82%E6%B2%99%E6%B2%B3%E5%8F%A3%E5%8C%BA%E4%BA%94%E4%B8%80%E8%B7%AF314&userId=0&longitude=0.0&user_id=0&latitude=0.0&language=zh";
        getInternetData();

    }

    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                AlbumMessageEntity entity = gson.fromJson(s, AlbumMessageEntity.class);
                if (entity != null && entity.getData() != null) {
                    data = entity.getData();
//                    setData();
                    albumMessageTitle.setText(data.getName());
                    albumName.setText(data.getName());
//                    publishTime.setText(data.getPublishDate());
                    albumType.setText(data.getTypeName());
                    albumLanguage.setText(data.getLang());
                    albumBrief.setText(data.getDescription());
                    Picasso.with(getActivity()).load(data.getPicUrl()).into(albumBigImg);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(request);
    }

    //向页面设置数据
    private void setData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.album_searching:

                getFragmentManager()
                        .beginTransaction()
                        .addToBackStack(null)
                        .replace(R.id.fl_main, new SearchResultFragment())
                        .commit();
                break;
            case R.id.album_message_back:
                getFragmentManager().popBackStack();

                break;
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getFragmentManager()
                .beginTransaction()
                .remove(AlbumMessageFragment.this)
                .commit();
    }
}
